package com.ydkj.a37e_mall.Resource.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.ydkj.a37e_mall.Classes.Model.Request.SearchInfo;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/19.
 */
public class DataHelper {
    // 数据库名称
    private static String DB_NAME = "mall.db";
    // 数据库版本
    private static int DB_VERSION = 2;
    private SQLiteDatabase db;
    private SqliteHelper dbHelper;

    public DataHelper(Context context) {
        dbHelper = new SqliteHelper(context, DB_NAME, null, DB_VERSION);
        db = dbHelper.getWritableDatabase();
    }

    public void Close() {
        db.close();
        dbHelper.close();
    }


    //添加录音(VoiceInfo)表的记录
    public long SaveVoiceInfo(SearchInfo searchInfo) {
        ContentValues values = new ContentValues();
        values.put(SearchInfo.NAME, searchInfo.getSearchname());
        long uid = db.insert(SqliteHelper.NEWSS_NAME, SearchInfo.ID, values);
        return uid;
    }

    // 获取录音(VoiceInfo)表中的记录
    public List<SearchInfo> GetVoiceInfo() {
        List<SearchInfo> searchList = new ArrayList<SearchInfo>();
        Cursor cursor = db.query(SqliteHelper.NEWSS_NAME, null, null, null, null,
                null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            SearchInfo searchInfo = new SearchInfo();
            searchInfo.setId(cursor.getString(0));
            searchInfo.setSearchname(cursor.getString(1));
            searchList.add(searchInfo);
            cursor.moveToNext();
        }
        cursor.close();
        return searchList;
    }

    //根据图片的ID删除录音(VoiceInfo)表中的记录
    public int DeVoiceInfo(String uid) {
        int id = db.delete(SqliteHelper.NEWSS_NAME,
                SearchInfo.ID + "=?", new String[]{uid});
        Log.e("DelUserInfo", id + "");
        return id;
    }

    //删除录音(VoiceInfo)表中所有记录
    public int DeALLVoiceInfo() {
        int id = db.delete(SqliteHelper.NEWSS_NAME,
                null, null);
        Log.e("DelUserInfo", id + "");
        return id;
    }


}
